package com.sch.www.ele.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.codeGenerator.base.CustomPage;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.core.web.BaseController;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.ele.entity.CareGroup;
import com.sch.www.ele.services.CareGroupServices;
import com.sch.www.ele.services.CareUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("v1/careGroup")
@RestController
public class CareGroupController extends BaseController {


    @Autowired
    CareGroupServices careGroupServices;
    @Autowired
    CareUserServices careUserServices;

    @RequestMapping(value = "list")
    private ResultBean<CustomPage<CareGroup>> list(
            String name,
            String regionCode,
            int pageNum,
            int pageSize
    ) {
        ResultBean<CustomPage<CareGroup>> resultBean = ResultBean.newInstance();
        CustomPage<CareGroup> page = careGroupServices.list(name, regionCode, pageNum, pageSize);
        resultBean.setSuccess().setData(page);
        return resultBean;
    }


    @PostMapping("add")
    public ResultBean<CareGroup> addGroup(@RequestBody CareGroup careGroup) {
        ResultBean<CareGroup> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careGroup.getName(), new NotBlankFluentValidator("name"))
                .on(careGroup.getLeaderUserId(), new NotBlankFluentValidator("leaderUserId"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careGroupServices.insertSelective(careGroup.getName(), careGroup.getLeaderUserId());
        return resultBean;
    }


    @PostMapping("modify")
    public ResultBean<CareGroup> modifyGroup(@RequestBody CareGroup careGroup) {

        ResultBean<CareGroup> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careGroup.getId(), new NotBlankFluentValidator("id"))
                .on(careGroup.getName(), new NotBlankFluentValidator("name"))
                .on(careGroup.getLeaderUserId(), new NotBlankFluentValidator("leaderUserId"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careGroupServices.updateByIdSelective(careGroup.getId(), careGroup.getName(), careGroup.getLeaderUserId());
        return resultBean;
    }


}
